home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / CD CHIP.ISO / WebServ / server7 / bin / ISAPIGetAndPost.dpr < prev    next >
Encoding:
Text File  |  1996-07-03  |  6.3 KB  |  230 lines

  1. library ISAPIGetAndPost;
  2.  
  3. uses
  4.   Windows,
  5.   SysUtils,
  6.   Classes,
  7.   ISAPISock,
  8.   Httpext,
  9.   Parser;
  10.  
  11. procedure ProcessGet(sock: TISAPISock);
  12. var
  13.   fin: TextFile;
  14.   s: String;
  15. begin
  16.   try
  17.     with sock do
  18.     begin
  19.       // Send a HTML header
  20.       Writeln('HTTP/1.0 200 OK');
  21.       Writeln('Content-type: text/html');
  22.       // Make this document expire immediately
  23.       Writeln('Expires: 0');
  24.       Writeln('');
  25.  
  26.       // Start the HTML Document
  27.       HHeader( 'PageBoy Database: Remote Configuration', hcLtGray, hcBlack, hcBlue );
  28.       HPageStart;
  29.  
  30.       HSeparator;
  31.       HImage( 'pageboy.gif' );
  32.       HHeading(1,'PageBoy Database: Remote Configuration');
  33.       HSeparator;
  34.  
  35.       //HLine( HBold('NOTE:') + ' PageBoy will automatically use this centralized database if you choose "Database-Remote" from the PageBoy menu.' );
  36.  
  37.       //HSeparator;
  38.  
  39.       Writeln( HBold(HItalic('Instructions: '))+'To add another user to the PageBoy database, fill out the form below and click on the submit button. This will allow all users of PageBoy');
  40.       Writeln(' to then use that entry if they choose '+HBold('DATABASE-REMOTE')+' from the PageBoy menu.');
  41.  
  42.       HFormStart('POST', '/bin/ISAPIGetAndPost.dll');
  43.  
  44.       HEditBox('   Last Name: ', 'LastName',    '', 50, 20);
  45.       HEditBox('  First Name: ', 'FirstName',   '', 50, 20);
  46.       HEditBox('Pager Number: ', 'PagerNumber', '', 50, 20);
  47.  
  48.       HFormEnd('Submit','');
  49.       HSeparator;
  50.       HLine( HCenter(HFontSize(HBold('Current Entries'), 5)) );
  51.       HLine('*****');
  52.       AssignFile(fin, ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  53.       try
  54.         Reset(fin);
  55.         try
  56.           while NOT EOF(fin) do
  57.           begin
  58.             System.ReadLn(fin, s);
  59.             HLine(s);
  60.           end;
  61.         finally
  62.           CloseFile(fin);
  63.         end;
  64.       except
  65.         // Something went wrong reading in the DB
  66.         HLine('An error occurred while reading the database '+ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  67.       end;
  68.       HLine('*****');
  69.       HSeparator;
  70.       HSeparator;
  71.       HPageEnd;
  72.     end;
  73.   except
  74.      // Something went wrong performing the GET
  75.   end;
  76. end;
  77.  
  78. procedure ProcessPost(sock: TISAPISock);
  79. var
  80.   fin, fout: TextFile;
  81.   database: TStringList;
  82.   s, token, field, value: String;
  83.   i: Integer;
  84.   lastName, firstName, pager: String;
  85. begin
  86.   database:=TStringList.Create;
  87.  
  88.   try
  89.     with sock do
  90.     begin
  91.       // Make the string list sorted, and ignore/reject duplicates
  92.       database.Sorted:=True;
  93.       database.Duplicates:=dupIgnore;
  94.  
  95.       // Send a HTML header
  96.       Writeln('HTTP/1.0 200 OK');
  97.       Writeln('Content-type: text/html');
  98.       Writeln('Expires: 0');
  99.       Writeln('');
  100.  
  101.       // Parse out the post
  102.       lastName:= GetFormVal('LastName');
  103.       firstName:=GetFormVal('FirstName');
  104.       pager:=    GetFormVal('PagerNumber');
  105.  
  106.       // Check if it's valid
  107.       if (lastName<>'') AND (firstName<>'') AND (pager<>'') then
  108.       begin
  109.  
  110.         // Tell the user he did good
  111.         HHeader( 'PageBoy Data Accepted', hcLtGray, hcBlack, hcBlue );
  112.         HPageStart;
  113.  
  114.         HSeparator;
  115.         HHeading(1,'PageBoy Data Accepted.');
  116.         HSeparator;
  117.  
  118.         // Show it to the user
  119.         HLine( HBold('Name: ')+lastName+', '+firstName);
  120.         HLine( HBold('Pager: ')+pager);
  121.  
  122.         HLine('Select '+HBold('DATABASE-REMOTE')+' from the PageBoy menu to reflect this change in your copy of PageBoy.');
  123.         HSeparator;
  124.  
  125.         // Add it to the database
  126.         database.Add(lastName+', '+firstName+' ('+pager+')');
  127.  
  128.         // Read in the existing data
  129.         AssignFile(fin, ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  130.         try
  131.           Reset(fin);
  132.           try
  133.             while NOT EOF(fin) do
  134.             begin
  135.               System.ReadLn(fin, s);
  136.               database.Add(s);
  137.             end;
  138.           finally
  139.             CloseFile(fin);
  140.           end;
  141.         except
  142.           // Something went wrong reading in the DB
  143.           HLine('An error occurred while reading the database '+ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  144.         end;
  145.  
  146.         // Now write it back out, including the new entry
  147.         AssignFile(fout, ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  148.         try
  149.           Rewrite(fout);
  150.           try
  151.             for i:=0 to database.Count-1 do
  152.             begin
  153.               System.Writeln(fout, database[i]);
  154.             end;
  155.           finally
  156.             CloseFile(fout);
  157.           end;
  158.         except
  159.            HLine('An error occurred while reading the database '+ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  160.         end;
  161.       end
  162.       else
  163.       begin
  164.         // Tell the user he goofed
  165.         HHeader( 'PageBoy Data Rejected', hcLtGray, hcBlack, hcBlue );
  166.         HPageStart;
  167.  
  168.         HSeparator;
  169.         HHeading(1,'PageBoy Data Rejected.');
  170.         HSeparator;
  171.         HLine('All fields must be filled in!');
  172.         HSeparator;
  173.       end;
  174.  
  175.       HPageEnd;
  176.     end;
  177.   finally
  178.     database.Free;
  179.   end;
  180. end;
  181.  
  182. // CASE MATTERS FOR THIS FUNCTION NAME
  183. function GetExtensionVersion(var ver: THSE_VERSION_INFO): Boolean; stdcall;
  184. begin
  185.   result:=True;
  186. end;
  187.  
  188. // CASE MATTERS FOR THIS FUNCTION NAME
  189. function HttpExtensionProc(var ecb: TEXTENSION_CONTROL_BLOCK): LongInt; stdcall;
  190. var
  191.   sock: TISAPISock;
  192.   method: String;
  193. begin
  194.   // Create the socket helper
  195.   sock:=TISAPISock.Create(ecb);
  196.  
  197.   method:=sock.GetServerVariable('REQUEST_METHOD');
  198.   if method='GET' then
  199.     ProcessGet(sock)
  200.   else if method='POST' then
  201.   begin
  202.     ProcessPost(sock)
  203.   end  
  204.   else
  205.   begin
  206.     sock.Writeln('HTTP/1.0 200 OK');
  207.     sock.Writeln('Content-type: text/html');
  208.     sock.Writeln('');
  209.     sock.Writeln('I didn''t understand that request');
  210.   end;
  211.  
  212.  
  213.   // Return a normal status code
  214.   StrLCopy( ecb.lpszLogData, PChar('DLL Finished with no errors'), HSE_LOG_BUFFER_LEN-1);
  215.   Result:=HSE_STATUS_SUCCESS;
  216.  
  217.   // Free the socket
  218.   sock.Free;
  219. end;
  220.  
  221. // * REQUIRED FOR DYNAMIC BINDING.
  222. // * Index values aren't need.
  223. // * Case doesn't matter here.
  224. exports
  225.   GetExtensionVersion,
  226.   HttpExtensionProc;
  227.  
  228. begin
  229. end.
  230.